import type { App } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'

import defaultImg from '@/assets/images/200.png'

export default {
  install(app: App) {
    app.directive('imgLazy', {
      mounted(el, binding) {
        const { stop } = useIntersectionObserver(
          el,
          ([{ isIntersecting }]) => {
            if (isIntersecting) {
              el.onerror = () => {
                el.src = defaultImg
              }

              el.src = binding.value
            }
          },
          {
            threshold: 0.01
          }
        )
      }
    })
  }
}
